Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cached-factory

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cached-factory

Creates and caches values under keys. 🏭

  • 0.0.2
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

cached-factory

Creates and caches values under keys. 🏭

All Contributors: 1 Codecov Test Coverage Contributor Covenant License: MIT Style: Prettier TypeScript: Strict

Usage

cached-factory exports a CachedFactory class that takes in "factory" function in its constructor. Each time a factory's .get(key) is called with any key for the first time, that factory is used to create a value under the key.

const cache = new CachedFactory((key) => `Cached: ${key}!`);

// "Cached: apple!"
cache.get("apple");

Values are cached so that subsequent .get(key) calls with the same key instantly return the same value.

const cache = new CachedFactory((key) => ({ key }));

// { key: "banana" }
cache.get("banana");

// true
cache.get("banana") === cached.get("banana");

Asynchronous Factories

CachedFactory does not itself handle Promise logic, but it doesn't have to! Provided factory functions can themselves be async / return Promise values.

const cache = new CachedFactory(
	async (key) => await fetch(`/some/resource?key=${key}`),
);

// Type: Promise<Response>
cache.get("cherry");

// Type: Response
await cache.get("cherry");

Other Methods

clear

Clears the cache.

cache.clear();

TypeScript

CachedFactory is written in TypeScript and ships with strong typing. 💪

👉 Tip: if you're working with noImplicitAny enabled (which is generally a good idea), an inline function provided as an argument to CachedFactory may need an explicit type annotation for its key.

new CachedFactory((key: string) => `Cached: ${key}!`);

Contributors

Josh Goldberg ✨
Josh Goldberg ✨

💻 🖋 📖 🤔 🚇 🚧 📆 🔧

💙 This package is based on @JoshuaKGoldberg's template-typescript-node-package.

FAQs

Package last updated on 04 Sep 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc